package com.zhiyinlou.apps.testcase.base;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.TreeMap;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.DataProvider;

import com.zhiyinlou.apps.util.file.ParsePropertiesUtil;
import com.zhiyinlou.apps.util.file.ParseXLSXUtil;





public class BaseCase {
	protected static Logger logger = LogManager.getLogger(BaseCase.class);
	// 配置文件config.xml
	public static Properties prop = ParsePropertiesUtil.getInstance();
	// 测试数据文件，保存在TestData下
	public String testfile;
	public static List<List<String>> lst = new ArrayList<List<String>>();
	
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	private static Process proc;
	
	
	
	// 保存测试结果的map
	public static TreeMap<Long, Integer> resultsMap;


	public BaseCase() {
		logger.info("[case]初始化类:" + this.getClass().getName());
		// 测试数据文件，文件名字必须和testcase的类名相同
		testfile = "TestData" + File.separator + this.getClass().getName().replace(".", File.separator) + ".xlsx";
		resultsMap = new TreeMap<Long, Integer>();
	}

	// 测试数据保存到二维数组testdata，sheet的名字必须和testng的测试方法相同都为test
	@DataProvider(name = "dataSource")
	public Object[][] dataSource(Method method) {
		logger.info("[BaseCase]开始准备测试数据...");
		Object[][] testdata = null;
		// 测试方法默认为test
		testdata = ParseXLSXUtil.getDataValue(testfile, "test");		
		return testdata;
	}

	@BeforeSuite
	public void init() {
		logger.info("[BaseCase]暂时保留...");
		String os = System.getProperties().getProperty("os.name");
		
		

	}

	@BeforeClass
	public void init_fields() {
		logger.info("================================================================");

		logger.info("[BaseCase]开始执行case: " + this.getClass().getName());
		logger.info("[BaseCase]初始化business...");
		

	}
	@BeforeMethod
	public void init_program() {
		String appPath=ParsePropertiesUtil.getInstance().getProperty("app_path");
		try {
			proc=Runtime.getRuntime().exec(appPath);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}
	
	@AfterMethod
	public void release_program() {	
		proc.destroy();
		
	}
	@AfterClass
	public void after_class() {
		logger.info("[BaseCase]暂时保留..");	
		
		
	}
	@AfterSuite
	public void closeApp() {
		

	}



}
